<template>
  <div class="goods">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/Home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card class="box-card">
      <el-row :gutter="20">
        <!-- 搜索区域 -->
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getGoodslist"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodslist"
            ></el-button>
          </el-input>
        </el-col>
        <!-- 添加用户 -->
        <el-col :span="4">
          <el-button type="primary" class="addUsers" @click="goAddpage"
            >添加商品</el-button
          >
        </el-col>
      </el-row>

      <!-- 用户信息表格 -->
      <el-table :data="goodsList" stripe border>
        <!-- 设置索引列 -->
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column
          prop="goods_price"
          label="商品价格(元)"
          width="110px"
        ></el-table-column>
        <el-table-column
          prop="goods_weight"
          label="商品重量"
          width="80px"
        ></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="180px">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>

        <el-table-column label="操作" width="130px">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              class="green_btn"
              @click="showEditDialog(scope.row.goods_id)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              class="red_btn"
              @click="removeGoodsById(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </el-card>
    
  </div>
</template>

<script>
export default {
  name: "goods",
  data() {
    return {
      queryInfo: {
        // *搜索的关键字
        query: "",
        // *当前的页数
        pagenum: 1,
        // *当前每页显示的数据条数
        pagesize: 5,
      },
      // *商品列表
      goodsList: [],
      // *商品列表数据总数
      total: 0,
      //   *控制添加商品对话框的显示与隐藏
      addDialogVisible: false,
    };
  },
  created() {
    this.getGoodslist();
  },
  methods: {
    // a 获取用户列表数据的函数 在created时调用
    async getGoodslist() {
      const res = await this.$http.get("goods", { params: this.queryInfo });
      if (res.data.meta.status != 200) {
        return this.$message.error("获取商品列表失败！");
      }
      this.goodsList = res.data.data.goods;
      this.total = res.data.data.total;
      this.pagenum = res.data.data.pagenum;
    },
    // *一个页面有几条 n条/页 修改时的事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getGoodslist();
    },
    // *当前在第几页的监听事件
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getGoodslist();
    },
    //
    showEditDialog() {},
    //  *单击删除按钮删除用户
    async removeGoodsById(id) {
      const removeGoods = await this.$confirm(
        "此操作将永久删除该商品, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((erro) => erro);
      if (removeGoods !== "confirm") {
        return this.$message.info("已取消删除");
      }
      // a 发起删除用户请求
      const res = await this.$http.delete("goods/" + id);
      if (res.data.meta.status !== 200) {
        this.$message.error("删除用户失败!");
      }
      this.$message.success("已成功删除该商品！"); //* 提示删除成功
      this.getGoodslist(); //*更新商品列表
    },
    // a添加商品跳转页面
    goAddpage() {
      // *push接受的参数是网页上的路径 不是路由的文件位置
      this.$router.push("/goods/add");
    },
  },
};
</script>

<style scoped lang="less">
</style>
